package com.woniuxy.portal.controller;

import com.woniuxy.portal.common.enumeration.Assert;
import com.woniuxy.portal.common.enumeration.BusinessCode;
import com.woniuxy.portal.model.dto.CartVo;
import com.woniuxy.portal.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.portal.util.JwtTemplate;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车接口")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private JwtTemplate jwtTemplate;


    @GetMapping
    @ApiOperation("查询购物车")
    public ResponseEntity<CartVo> getCart(@RequestHeader(name = "Authorization", required = false) String token) {
        Assert.error(!jwtTemplate.validateToken(token), BusinessCode.INVALID_TOKEN);
        String userId = jwtTemplate.getValue(token, "userId");
        log.info("用户ID:{}", userId);
        CartVo cart = cartService.getCart(Long.valueOf(userId));
        log.info("购物车信息:{}", cart);
        return ResponseEntity.ok(cart);
    }

}
